package com.ajavaer.framework.common.message;


import com.ajavaer.framework.common.tools.JsonTools;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 系统消息包装类
 * <p>code:
 * <ul>
 * <li>SUCCESS表示正常，msg为""</li>
 * <li>code遵循http状态码(200为正常)，msg为异常信息</li>
 * <p>
 * </ul></p>
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Message<T> implements Serializable {
    /**
     * 提示信息
     */
    private String msg = "OK";
    /**
     * 遵循http状态码(200为正常)
     */
    private int code = HttpStatus.OK.value();
    /**
     * 返回数据
     */
    private T data;
    /**
     * 服务端时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public static final Message OK = new Message(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());

    public Message() {
    }

    public Message(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Message(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Message(ErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public static Message me() {
        return new Message();
    }

    public Message msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Message code(int code) {
        this.code = code;
        return this;
    }

    public Message data(T data) {
        this.data = data;
        return this;
    }

    public boolean isSuccess() {
        return this.code == HttpStatus.OK.value();
    }

    public static <T> Message<T> successResult(T data) {
        return new Message(HttpStatus.OK.value(), "", data);
    }

    public static Message success(String msg) {
        return new Message(HttpStatus.OK.value(), msg);
    }

    public static Message success() {
        return OK;
    }

    public static Message success(String msg, Object data) {
        return new Message(HttpStatus.OK.value(), msg, data);
    }

    public static Message fail(String msg) {
        return new Message(HttpStatus.NO_CONTENT.value(), msg);
    }

    public static Message fail(HttpStatus httpStatus, String msg) {
        return new Message(httpStatus.value(), msg);
    }

    public static Message fail(HttpStatus httpStatus) {
        return new Message(httpStatus.value(), httpStatus.getReasonPhrase());
    }

    public static Message fail(ErrorCode errorCode) {
        return new Message(errorCode);
    }

    public static Message fail(int code, String msg) {
        return new Message(code, msg);
    }

    public static Message fail(String msg, Object result) {
        return new Message(HttpStatus.NO_CONTENT.value(), msg, result);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return JsonTools.beanToJson(this);
    }
}
